﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;

namespace TravelexForwardContract
{
    public partial class MainForm : Form
    {
        private String m_strConn;      //Connection string
        private String m_TAllRecord;   //Table Name in database

        public MainForm()
        {
            InitializeComponent();
            m_strConn = "Data Source=HEEFAN-PC\\SQLEXPRESS;" +
                "Initial Catalog=TravelexForwardContract;" +
                "Integrated Security=true";
            m_TAllRecord = "allrecords";
        }

        public static int GetLineNum()
        {
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            return st.GetFrame(0).GetFileLineNumber();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            String queryComboItems = "select distinct Company, ClientId from " + m_TAllRecord;
            using (SqlDataAdapter sqlDA = new SqlDataAdapter(queryComboItems, m_strConn))
            {
                 try
                 {
                     DataSet dsComboBox = new DataSet();
                     sqlDA.Fill(dsComboBox, m_TAllRecord);
                     comboBoxCompany.DataSource = dsComboBox.Tables[m_TAllRecord];
                     comboBoxCompany.DisplayMember = "Company";
                     comboBoxClientId.DataSource = dsComboBox.Tables[m_TAllRecord];
                     comboBoxClientId.DisplayMember = "ClientId";
                 }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Close();
                 }
            }
        }

        private void buttonGenerateReport_Click(object sender, EventArgs e)
        {
            String strClientId = comboBoxClientId.Text;
            if (strClientId == "")
            {
                MessageBox.Show("ClientId is empty");
                this.Close();
            }
                  
            try
            {
                IndividualReportForm frmIndividual = new IndividualReportForm(strClientId);
                frmIndividual.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void newRecordsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void allRecordsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                AllReportForm formAllReport = new AllReportForm();
                formAllReport.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "AllReportForm Exception Message",
                 System.Windows.Forms.MessageBoxButtons.OK,
                 System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Close Exception Message",
                   System.Windows.Forms.MessageBoxButtons.OK,
                   System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        private void newRecordsToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            NewRecordsForm frmNewRecords = new NewRecordsForm();
            frmNewRecords.ShowDialog();
        }

        private void allRecordsToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            AllReportForm frmAllReport = new AllReportForm();
            frmAllReport.ShowDialog();
        }
    }
}

